# -*- coding: utf-8 -*-
from urllib import request
from http import cookiejar


class HtmlDownloader(object):

    def download(self , url):
        if url is None:
            return None
        print("开始下载页面：",url)
        headers = {'User-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}

        # 构造cookie
        cookie = cookiejar.CookieJar()

        # 由cookie构造opener
        opener = request.build_opener(request.HTTPCookieProcessor(cookie))
        req = request.Request(url, headers=headers)
        resp = opener.open(req)

        if resp.code == 200:
            return resp.read()#.decode("UTF-8")
        else:
            return None
